package com.example.mallapi.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.mallapi.common.Result;
import com.example.mallapi.entity.Category;
import com.example.mallapi.qo.CategoryQo;
import com.example.mallapi.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 类别表 前端控制器
 * </p>
 *
 * @author zhengpf
 * @since 2023-06-01
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    //分页查询相关信息
    @GetMapping("list")
    public Result list(CategoryQo qo){

        IPage<Category> page = categoryService.page(qo);
        return Result.success("查询成功",page);
    }

    //分类添加
    @PostMapping("insert")
    public Result insert(@RequestBody Map<String,Object> map){

        log.info("获取添加信息：{}",map);

        categoryService.save(map);

        return Result.success("分类信息添加成功");
    }

    //点击信息回显前端
    @GetMapping("info/{id}")
    public Result getInfo (@PathVariable Integer id){

        //查询到分类信息
        Category list = categoryService.getById(id);
        return Result.success("查询成功",list);
    }


    //修改
    @PutMapping("update")
    public Result update(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.success("分类信息修改成功");
    }

    //删除
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id){
        categoryService.removeById(id);

        return Result.success("删除成功");
    }

    //查询所有的产品分类
    @GetMapping("listAll")
    public Result listAll(){
        List<Category> list = categoryService.list();
        return Result.success("所有分类信息查询成功",list);
    }


    /**
     * 待扩展：批量删除接口
     */
}



